package com.hmy.common.utils;

import java.io.File;
import java.text.MessageFormat;
import java.util.*;

/**
 *
 * <p>
 * Title:读取国际化资源文件
 * </p>
 * <p>
 * Description:资源文件读取类，该类只使用静态方法
 * </p>
 */
public class Messages {

	/**
	 * 资源内容存储
	 */
	private static Map messages = null;

	/**
	 * 是否逃
	 */
	public static boolean isEscape = false;

	/**
	 * 资源位置
	 */

	public static String RESOURCE_BUNDLE = "locale.resource";

	/**
	 *
	 */
	private static boolean isInitialised = false;

	/**
	 * 从资源文件中获取字符串，该方法利用PropertyResourceBoundle类的功能。
	 *
	 *
	 * @param key
	 *            资源中的键名。
	 *
	 *
	 * @return 和该键名对应的资源字符串。若找不到key对应的资源则返回key本身。
	 *
	 *
	 */
	private static final Object LOCK = new Object();

	public static String get(String key) {

		if (messages == null || !isInitialised) {
			synchronized (LOCK) {
				if (!isInitialised){
				    init();
				}
			}
		}
		try {
			MessageFormat str = (MessageFormat) messages.get(key);
			return str.format(null);
		} catch (NullPointerException mre) {
			return key;
		} catch (Exception ex) {
			return key;
		}
	}

	/**
	 * 从资源文件中获取字符串，并替换需要更改的参数 如 a.b.c=d{0}fg{1} get("a.b.c",{"1","2"})="d1fg2"
	 *
	 * @param key
	 *            资源中的键名
	 * @param s
	 *            需要替换的字符
	 * @return 和该键名对应的资源字符串。并替换需在更改的参数,若找不到key对应的资源则返回key本身。
	 */
	public static String get(String key, String s[]) {
		if (messages == null || !isInitialised) {
			synchronized (LOCK) {
				if (!isInitialised){
				    init();
				}
			}
		}
		MessageFormat format = null;
		try {
			format = (MessageFormat) messages.get(key);
		} catch (Exception mre) {
			format = null;
		}
		if (format == null) {
			synchronized (Messages.class) {
				format = new MessageFormat(escape(key));
				messages.put(key, format);
			}
		}
		return format.format(s);
	}

	public static String get(String key, String arg) {
		return get(key, new String[] { arg });
	}

	public static String get(String key, String arg1, String arg2) {
		return get(key, new String[] { arg1, arg2 });
	}

	public static String get(String key, String arg1, String arg2, String arg3) {
		return get(key, new String[] { arg1, arg2, arg3 });
	}

	private static String escape(String string) {

		if (!isEscape) {
			return string;
		}

		if ((string == null) || (string.indexOf('\'') < 0)) {
			return string;
		}

		int n = string.length();
		StringBuffer sb = new StringBuffer(n);

		for (int i = 0; i < n; i++) {
			char ch = string.charAt(i);

			if (ch == '\'') {
				sb.append('\'');
			}
			sb.append(ch);
		}
		return sb.toString();
	}

	/**
	 * = 初始化
	 *
	 */
	private static void init() {
		// 暂时不加载国际化文件
		if (!new File(RESOURCE_BUNDLE).exists()) {
			isInitialised = true;
			return;
		}
		ResourceBundle resource = ResourceBundle.getBundle(RESOURCE_BUNDLE,Locale.getDefault());
		messages = new HashMap();
		Enumeration elements = resource.getKeys();
		while (elements.hasMoreElements()) {
			String key = (String) elements.nextElement();
			MessageFormat value = new MessageFormat(resource.getString(key));
			messages.put(key, value);

		}
		isInitialised = true;
	}

}
